require "import"
import "str"

--pid=...

--rc4=import "mods.minicrypto"

--import "android.support.v4.app.*"
--import "android.support.v4.view.*"
import "android.support.v4.widget.*"

topbar_click=0

--this.setDebug(true)

local refresh
--local year=os.date("%Y")+0
local date=os.date("%m月%d日")
:gsub("%d+", function(s)
--print(s)
return tointeger(s)
end)
--print(date)

local data={}
local adp=LuaMultiAdapter(this,data,{
{
TextView,
textColor=文字色,
id="title",
textSize="16sp",
layout_width=-1,
padding="16dp",
},
{
LinearLayout,
orientation=1,
padding="16dp",
layout_width=-1,
{
FrameLayout,
--gravity=17,
layout_width=-1,
{
TextView,
textColor=文字色,
id="title",
backgroundDrawable=圆角(nil,文字色-0xa0000000,{36,36,36,36,36,36,36,36}),
textSize="24sp",
padding="6dp",
paddingTop=0,
paddingBottom=0,
},
{
ImageView,
layout_gravity=5|17,
src="drawable/arrow.png",
--rotation=90,
layout_height="24dp",
layout_width="24dp",
colorFilter=图标色,
},
},
{
LinearLayout,
layout_width=-1,
layout_marginTop="4dp",
gravity=17,
{
TextView,
textColor=次要文字色,
id="subtitle",
textSize="16sp",
layout_weight=1,
},
{
TextView,
textColor=文字色,
id="action",
padding="6dp",
paddingTop="3dp",
paddingBottom="3dp",
onClick=function()
date=os.date("%m月%d日")
:gsub("%d+", function(s)
--print(s)
return tointeger(s)
end)
refresh()
end,
text="回到今天",
textSize="16sp",
backgroundDrawable=波纹(波纹色,圆角(nil,-1,{20,20,20,20,20,20,20,20}))
},
},
},
{
TextView,
layout_width=-1,
textColor=次要文字色,
--id="title",
textSize="14sp",
padding="32dp",
gravity=17,
text="今天发生了什么事呢？"
},
{
TextView,
layout_width=-1,
minimumHeight="500dp",
textColor=次要文字色,
id="title",
textSize="16sp",
gravity=17,
--text="数据加载失败"
}
})

this.setContentView(loadlayout ({
  RelativeLayout,
  --layout_width="fill",
  --layout_height="fill",
  --visibility=4,
  backgroundColor=背景色,
  --orientation="vertical",
  --elevation="2%w",
  paddingTop=状态栏高度,
  {
    RelativeLayout,
    layout_width="fill",
    layout_height="56dp" ,
    gravity="center",
    padding="16dp",
    --backgroundColor=0xffffffff,
    elevation="2dp",
    paddingTop="8dp",
    paddingBottom="8dp",
    onClick=function()
      if topbar_click+2 > tonumber(os.time()) then
        list.smoothScrollToPosition(0,0)
       else
        topbar_click=tonumber(os.time())
      end
    end;
    {
      ImageView,
      src="drawable/back.png",
      layout_height="fill",
      layout_width="44dp",
      foreground=波纹(波纹色),
      onClick=function ()
        this.finish()
      end,
      padding="9dp",
      colorFilter=图标色,
    },
    
    {
      TextView,
      layout_width="fill",
      layout_height="fill",
      gravity="center",
      --layout_gravity="center",
      text="历史上的今天",
      textSize="20sp",
      textColor=文字色,
    },
  },
  {
  SwipeRefreshLayout,
  id="swipe",
  layout_width=-1,
  layout_height=-1,
    {
      ListView,
      layout_width=-1,
      layout_height=-1,
      paddingTop="56dp",
      clipToPadding=false,
      id="list",
      adapter=adp,
      onItemLongClickListener={
        onItemLongClick=function(l,v,p)
          local isText=luajava.instanceof(v,TextView)
          if isText&&v.getGravity()==17 then
            refresh()
           elseif !isText then
            return true
          end
          local edt=AlertDialog.Builder(this)
          -- .setCancelable(false)
          .setTitle("选择操作")
          .setItems({"添加收藏","分享内容","复制内容"}, function (d,n)
          local hiscon=v.getText():gsub("年：",os.date("年 %m 月 %d 日："),1)
          if n==1 then
            
            textn=hiscon.."\n    ——闲言APP "..slogan
            shareText(textn)
           elseif n==2 then
            copyText(hiscon.."\n    ——闲言APP "..slogan)
           elseif n==0 then
            io.open(句子收藏..os.time().."("..#hiscon..")","w+"):write([[{
soup=]].."[["..hiscon.."]]"..[[,
type=]].."[[历史上的今天]]"..[[,
}]]):close()
            toast ("已加收藏，可在灵感中管理")
          end
        end)
        local ed=edt.show()
        local pw=ed.getWindow()
        --.setWindowAnimations(R.style.BottomDialog_Animation)
        --.setBackgroundDrawable(ColorDrawable(0))
        .setGravity(Gravity.BOTTOM)
        圆角(pw,背景色,{0,0,0,0,0,0,0,0})
        local lp=pw.getAttributes()
        lp.width=w
        pw.setAttributes(lp)
        pw.setDimAmount(0.35)
        return true
      end
      },
      onItemClickListener={
        onItemClick=function(l,v,p)
          local isText=luajava.instanceof(v,TextView)
          if isText&&v.getGravity()==17 then
            refresh()
           elseif !isText then
            DatePickerDialog(this,{
              onDateSet=function(_,y,m,d)
                date=(m+1).."月"..d.."日"
                refresh()
              end
              },
              os.date("%Y")+0,--year,
              date:match("(.-)月")-1,
              date:match("月([%d]+)")+0
            )
            .show()
          end
        end
      },
    },
  },
  {
    LinearLayout,
    layout_width="fill",
    layout_height="56dp" ,
    foreground=上下渐变({背景色,深透,淡透}),
  },
}))

function refresh()
--Http.get("http://jieri.jpszx.com/day/thin.php", function(c,n)
Http.get("http://jieri.jpszx.com/today/"..date..".json", function(c,n)
  if c!=200 then
    toast("数据加载失败")
    if #data==0 then
      table.insert(data,{
        __type=4,
        title="数据加载失败"
      })
      adp.notifyDataSetChanged()
    end
   else
    local isToday=date==os.date("%m月%d日")
                        :gsub("%d+", function(s)
                          --print(s)
                          return tointeger(s)
                        end)
    table.clear(data)
    table.insert(data,{
        __type=2,
        action={
          visibility=isToday&&8||0
        },
        title=isToday
          &&os.date("%d"):gsub("%d+", function(s)
             --print(s)
             return tointeger(s)
           end)
          ||date:match("([%d]+)日"),
        subtitle=isToday
          &&os.date("%Y 年 %m 月•今天"):gsub("%d+", function(s)
               --print(s)
               return tointeger(s)
             end)
          ||date:gsub("[%d]+日","",1)
      })
    local s=cjson.decode(n)
    --for y,s in n:gmatch("([%d]+)年：(.-)。") do
    local events=s.大事记
    if events&&#events>0 then
     for _,v in ipairs(events) do
        --print(y,s)
        table.insert(data,{
          __type=1,
          --title=y.." 年："..s.."。"
          title=v
        })
      end
     else
      table.insert(data,{
        __type=4,
        title="今天无大事发生"
      })
    end
    table.insert(data,{
      __type=3,
    })
    adp.notifyDataSetChanged()
  end
  swipe.setRefreshing(false)
end)
end

refresh()

swipe.setProgressViewOffset(true,dp2px(56),dp2px(32+56))
.setColorSchemeColors({文字色})
.setProgressBackgroundColorSchemeColor(背景色)
.setRefreshing(true)
.onRefresh=refresh
